using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class SolarSystem : EveObject
    {
        private int regionId;
        private int constellationId;
        private string solarSystemName;
        private double x, y, z;
        private double xMax, yMax, zMax;
        private double xMin, yMin, zMin;
        private double luminosity;
        private bool border;
        private bool fringe;
        private bool corridor;
        private bool hub;
        private bool international;
        private bool regional;
        private bool constellationCapital;
        private double security;
        private int factionId;
        private double radius;
        private int sunTypeId;
        private string securityClass;
        private int allianceId;
        private int sovereigntyLevel; // rev 2.0 only
        private DateTime sovereigntyDateTime; // rev 2.0 only

        private Region region;
        private Constellation constellation;
        private Faction faction;
        private Type sun;
        private EveObject alliance;
        private List<SolarSystem> jumps;
        private List<SecurityRating> securityRatings;

        public Region Region
        {
            get
            {
                if (region == null)
                    region = new Region(db, regionId);
                return region;
            }
        }
        public Constellation Constellation
        {
            get
            {
                if (constellation == null)
                    constellation = new Constellation(db, constellationId);
                return constellation;
            }
        }
        public string Name
        {
            get { return solarSystemName; }
        }
        public Vector Location
        {
            get { return new Vector(x, y, z); }
        }
        public Vector Minimum
        {
            get { return new Vector(xMin, yMin, zMin); }
        }
        public Vector Maximum
        {
            get { return new Vector(xMax, yMax, zMax); }
        }
        public double Luminosity
        {
            get { return luminosity; }
        }
        public bool Border
        {
            get { return border; }
        }
        public bool Fringe
        {
            get { return fringe; }
        }
        public bool Corridor
        {
            get { return corridor; }
        }
        public bool Hub
        {
            get { return hub; }
        }
        public bool International
        {
            get { return international; }
        }
        public bool Regional
        {
            get { return regional; }
        }
        public bool ConstellationCapital
        {
            get { return constellationCapital; }
        }
        public double Security
        {
            get { return security; }
        }
        public Faction Faction
        {
            get
            {
                if (faction == null)
                    faction = new Faction(db, factionId);
                return faction;
            }
        }
        public double Radius
        {
            get { return radius; }
        }
        public Type Sun
        {
            get
            {
                if (sun == null)
                    sun = new Type(db, sunTypeId);
                return sun;
            }
        }
        public string SecurityClass
        {
            get { return securityClass; }
        }
        public EveObject Alliance
        {
            get
            {
                if (alliance == null)
                    alliance = EveData.GetObject(db, allianceId);
                return alliance;
            }
        }
        public List<SolarSystem> Jumps
        {
            get
            {
                if (jumps == null)
                {
                    jumps = new List<SolarSystem>();
                    string query = "SELECT * FROM mapSolarSystemJumps WHERE fromSolarSystemID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        jumps.Add(new SolarSystem(db, row["toSolarSystemID"]));
                }
                return jumps;
            }
        }
        public List<SecurityRating> SecurityRatings
        {
            get
            {
                if (securityRatings == null)
                {
                    securityRatings = new List<SecurityRating>();
                    string query = "SELECT * FROM mapSecurityRatings WHERE fromSolarSystemID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        securityRatings.Add(new SecurityRating(row, this));
                }
                return securityRatings;
            }
        }
        public int SovereigntyLevel // rev 2.0 only
        {
            get { return sovereigntyLevel; }
        }
        public DateTime SovereigntyDateTime // rev 2.0 only
        {
            get { return sovereigntyDateTime; }
        }

        internal SolarSystem(DbDataRow dr)
            : base(dr.Database, EveObjectType.SolarSystem, dr["solarSystemID"])
        {
            Fill(dr);
        }
        internal SolarSystem(Database db, int id)
            : base(db, EveObjectType.SolarSystem, id)
        {
            Fill(db.ExecuteSingle("SELECT * FROM mapSolarSystems WHERE solarSystemID=" + id));
        }

        private void Fill(DbDataRow dr)
        {
            regionId = dr["regionID"];
            constellationId = dr["constellationID"];
            solarSystemName = dr["solarSystemName"];
            x = dr["x"];
            y = dr["y"];
            z = dr["z"];
            xMin = dr["xMin"];
            yMin = dr["yMin"];
            zMin = dr["zMin"];
            xMax = dr["xMax"];
            yMax = dr["yMax"];
            zMax = dr["zMax"];
            luminosity = dr["luminosity"];
            border = dr["border"];
            fringe = dr["fringe"];
            corridor = dr["corridor"];
            hub = dr["hub"];
            international = dr["international"];
            regional = dr["regional"];
            constellationCapital = dr["constellation"];
            security = dr["security"];
            factionId = dr["factionID"];
            radius = dr["radius"];
            sunTypeId = dr["sunTypeID"];
            securityClass = dr["securityClass"];
            allianceId = dr["allianceID"];
            sovereigntyLevel = dr["sovereigntyLevel"];
            sovereigntyDateTime = dr["sovereigntyDateTime"];
        }
    }
}
